package com.wangijun.yuan.admin.base;


import com.wangijun.yuan.core.base.ErrEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;

@Schema(description = "通用返回")
@Data
@Builder
@Accessors(chain=true)
public class CommonResult<T> {

    @Schema(description = "状态码（0：成功，其他失败）")
    private int code;
    @Schema(description = "消息描述")
    private String msg;
    @Schema(description = "返回的数据")
    private T data;

    public CommonResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 返回成功-提交类
     *
     */
    public static <T> CommonResult<T> ok() {
        return new CommonResult<>(0, "成功", null);
    }

    /**
     * 返回成功-提交类
     *
     */
    public static <T> CommonResult<T> ok(String msg) {
        return new CommonResult<>(0, msg, null);
    }

    /**
     * 返回成功-提交类
     *
     */
    public static <T> CommonResult<T> fail(int code, String msg) {
        return new CommonResult<>(code, msg, null);
    }

    /**
     * 返回成功-提交类
     *
     */
    public static CommonResult<?> fail(ErrEnum errEnum) {
        return fail(errEnum.getCode(), errEnum.getMessage());
    }

    /**
     * 返回成功-提交类
     *
     */
    public static CommonResult<?> fail(String msg) {
        return fail(99999999, msg);
    }



    /**
     * 功能描述: 抛出一个异常
     *
     * @author wlf 2024/3/23 13:16
     */
    public static void error(String msg) {
        throw new RuntimeException(msg);
    }

    /**
     * 返回成功-提交类
     *
     */
    public static <T> CommonResult<T> data(T data) {
        return new CommonResult<>(0, "成功", data);
    }
}
